package com.pajk.user.jmeter;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.pajk.user.api.UserService;
import com.pajk.user.model.User;
import com.pajk.user.model.enums.ChannelType;

public class UserService_getOrCreateUserByOpenId extends
		AbstractJavaSamplerClient {

	private String jMeterPrefix = null;
	private String jMeterOutput = null;

	private ClassPathXmlApplicationContext appContext = null;

	private UserService userService = null;

	private String openIdPrefix = null;
	private long openIdCount = 0L;

	private BufferedWriter bw = null;

	public static void main(String[] args) {

		Thread.currentThread().setName("Thread Group 1-1");
		JavaSamplerContext context = new JavaSamplerContext(new Arguments());
		UserService_getOrCreateUserByOpenId test = new UserService_getOrCreateUserByOpenId();
		test.setupTest(context);
		test.runTest(context);
		test.teardownTest(context);
	}

	public Arguments getDefaultParameters() {

		Arguments params = new Arguments();

		params.addArgument(Utility.JMETER_PREFIX_LABEL,
				Utility.JMETER_PREFIX_DEFAULT);
		params.addArgument(Utility.JMETER_OUTPUT_LABEL,
				Utility.JMETER_OUTPUT_DEFAULT);

		return params;
	}

	public void setupTest(JavaSamplerContext context) {

		super.setupTest(context);

		jMeterPrefix = context.getParameter(Utility.JMETER_PREFIX_LABEL,
				Utility.JMETER_PREFIX_DEFAULT);
		jMeterOutput = context.getParameter(Utility.JMETER_OUTPUT_LABEL,
				Utility.JMETER_OUTPUT_DEFAULT);

		appContext = new ClassPathXmlApplicationContext(
				Utility.USER_SERVICE_REFERENCE_DEFINITION);
		appContext.start();

		userService = (UserService) appContext.getBean("userService");

		String threadName = Thread.currentThread().getName();

		StringBuffer sb = new StringBuffer();
		sb.append(jMeterPrefix);
		sb.append('-');
		sb.append(threadName);
		sb.append('-');

		openIdPrefix = sb.toString();

		sb.append(Utility.USERIDS);
		sb.append(Utility.LOGEXT);

		String results = jMeterOutput + sb.toString();

		File dirs = new File(jMeterOutput);
		if (!dirs.exists()) {
			dirs.mkdirs();
		}

		try {
			bw = new BufferedWriter(new FileWriter(results, false));
		} catch (IOException e) {
			this.getLogger().error(e.getMessage(), e.getCause());
		}
	}

	@Override
	public SampleResult runTest(JavaSamplerContext arg0) {

		SampleResult sampleResult = new SampleResult();

		sampleResult.setDataEncoding(Utility.ENCODING);

		String openId = openIdPrefix + openIdCount++;

		User user = null;

		try {
			sampleResult.sampleStart();

			user = userService.getOrCreateUserByOpenId(openId,
					ChannelType.WEIXIN);

			sampleResult.setSuccessful(true);
		} catch (Exception e) {
			sampleResult.setSuccessful(false);
			this.getLogger().error(e.getMessage(), e.getCause());
		} finally {
			if (sampleResult != null) {
				sampleResult.sampleEnd();
				try {
					bw.write(openId);
					bw.write(":");
					if (user != null) {
						bw.write(Long.toString(user.getId()));
					} else {
						bw.write("null");
					}
					bw.write("\r");
					bw.flush();
				} catch (IOException e) {
					this.getLogger().error(e.getMessage(), e.getCause());
				}
			}
		}

		return sampleResult;
	}

	public void teardownTest(JavaSamplerContext context) {

		super.teardownTest(context);

		try {
			bw.close();
			bw = null;
		} catch (IOException e) {
			this.getLogger().error(e.getMessage(), e.getCause());
		}

		appContext.close();
	}

}